import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class ImageHelper {
  static int memCacheRatio = 3; //缓存图片内存缓存比率
  static int diskCacheRatio = 3; //缓存图片磁盘缓存比率

  ///加载网络图片
  ///memRatioEnable 默认3倍比例 图片尺寸和view尺寸差不多时开启 要不然会出现变形的问题
  static Widget loadNetImg(
    String imageUrl, {
    double? width,
    double? height,
    BoxFit? fit,
    Widget? placeholder,
    Widget? errorWidget,
    Alignment? alignment,
    double? radius,
    // bool canClearCache = true,
    Duration fadeInDuration = const Duration(milliseconds: 300),
    Curve? fadeOutCurve,
    Duration fadeOutDuration = const Duration(milliseconds: 300),
    Curve? fadeInCurve,
    bool memCacheEnable = false,
    int? maxWidthDiskCache,
    int? maxHeightDiskCache,
    bool? useOldImageOnUrlChange,
  }) {
    // PlaceholderWidgetBuilder
    Widget funPlaceholder(BuildContext context, String url) {
      return placeholder!;
    }

    // LoadingErrorWidgetBuilder
    Widget funLoadingError(BuildContext context, String url, dynamic error) {
      if (errorWidget != null) {
        return errorWidget;
      }
      return placeholder!;
    }

    Widget child = CachedNetworkImage(
      imageUrl: imageUrl,
      width: width,
      height: height,
      fit: fit,
      placeholder: placeholder != null ? funPlaceholder : null,
      errorWidget: (errorWidget != null || placeholder != null)
          ? funLoadingError
          : null,
      // cacheManager:
      //     canClearCache ? DefaultCacheManager() : UnClearCacheManager(),
      fadeInDuration: fadeInDuration,
      fadeOutDuration: fadeOutDuration,
      useOldImageOnUrlChange: useOldImageOnUrlChange ?? false,
      memCacheWidth: memCacheEnable
          ? getRatioSize(width, ratio: memCacheRatio)
          : null,
      memCacheHeight: memCacheEnable
          ? getRatioSize(height, ratio: memCacheRatio)
          : null,
      maxWidthDiskCache:
          maxWidthDiskCache ?? getRatioSize(width, ratio: diskCacheRatio),
      maxHeightDiskCache:
          maxHeightDiskCache ?? getRatioSize(height, ratio: diskCacheRatio),
    );

    if (radius != null) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: child,
      );
    }
    return child;
  }

  //获取缩放值
  static int? getRatioSize(double? size, {int ratio = 3}) {
    if (size != null && size != double.maxFinite && size != double.infinity) {
      return (size * ratio).toInt();
    }
    return null;
  }

  ///加载圆形的网络图片
  static Widget loadCircleNetImg(
    String imageUrl, {
    double? width,
    double? height,
    BoxFit? fit,
    Widget? placeholder,
    Widget? errorWidget,
    Alignment? alignment,
    BoxBorder? border,
    Color? bgColor,
    EdgeInsets? padding,
    List<BoxShadow>? boxShadow,
    int? maxWidthDiskCache,
    int? maxHeightDiskCache,
    bool? useOldImageOnUrlChange,
    String? package,
  }) {
    return Container(
      width: width,
      height: height,
      padding: padding,
      decoration: BoxDecoration(
        border: border,
        color: bgColor,
        shape: BoxShape.circle,
        boxShadow: boxShadow ?? [],
      ),
      child: ClipOval(
        child: loadNetImg(
          imageUrl,
          width: width,
          height: height,
          fit: fit,
          alignment: alignment,
          placeholder: placeholder,
          errorWidget: errorWidget,
          maxWidthDiskCache: maxWidthDiskCache,
          maxHeightDiskCache: maxHeightDiskCache,
        ),
      ),
    );
  }
}

/// 不会被直接清除的图片缓存地址
// class UnClearCacheManager extends CacheManager {
//   static const key = 'libCachedImageUnClearData';
//
//   static UnClearCacheManager? _instance;
//
//   factory UnClearCacheManager() {
//     _instance ??= UnClearCacheManager._();
//     return _instance!;
//   }
//
//   UnClearCacheManager._() : super(Config(key));
//
//   @override
//   Future<String> getFilePath() async {
//     var directory = await getTemporaryDirectory();
//     return path.join(directory.path, key);
//   }
// }
